document.addEventListener('DOMContentLoaded', function() {
  const redirectButton = document.getElementById('redirectButton');
  const statusDiv = document.getElementById('status');

  // 获取当前标签页并检查状态
  chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
    const currentTab = tabs[0];
    const url = new URL(currentTab.url);

    // 根据域名设置状态和按钮
    if (url.hostname === "sanhai.sh3.ikuai7.com") {
      statusDiv.textContent = "准备跳转至新域名";
      redirectButton.disabled = false;
    } else if (url.hostname === "sanhaioj.longzhuge.com.cn") {
      statusDiv.textContent = "已在新域名";
      redirectButton.disabled = true;
    } else {
      statusDiv.textContent = "非目标域名";
      redirectButton.disabled = true;
    }
  });

  // 点击跳转按钮
  redirectButton.addEventListener('click', function() {
    chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
      const currentTab = tabs[0];
      const url = new URL(currentTab.url);

      if (url.hostname === "sanhai.sh3.ikuai7.com") {
        // 提取路径并构建新URL
        const path = url.pathname + url.search + url.hash;
        const newUrl = "https://sanhaioj.longzhuge.com.cn" + path;

        // 执行跳转
        chrome.tabs.update(currentTab.id, { url: newUrl });
        statusDiv.textContent = "正在跳转...";
        redirectButton.disabled = true;
      }
    });
  });
});